/* rqrqxr.p  - Requistions ROP                                                */
/* Copyright 1986-2002 QAD Inc., Carpinteria, CA, USA.                        */
/* All rights reserved worldwide.  This is an unpublished work.               */
/* $Revision: 1.3 $                                                           */
/*                                                                            */
/* This routine provides the procedures which are executed as a result of the */
/* call from the appropriate Data Type Controllers / structured programs      */
/* It contains business logic pertaining to Requisitions in general, it is not*/
/* specific to Standard or Global style requisitions. This in effect acts as  */
/* a sub-datatype controller for requisitions, handling general processing    */
/* common to all requisitions as routing processing to either GRS or Standard */
/* requisition ROPs.                                                          */
/*                                                                            */
/* Revision: 1.1       BY:Bill Pedersen            DATE:04/25/00  ECO: *N059* */
/* Revision: 1.2       BY:Zheng Huang              DATE:10/20/00  ECO: *N0T2* */
/* $Revision: 1.3 $       BY: Murali Ayyagari         DATE: 11/06/00 ECO: *N0V1* */
/*                                                                            */
/*V8:ConvertMode=NoConvert                                                    */

/* ========================================================================== */
/* ******************************* DEFINITIONS ****************************** */
/* ========================================================================== */

/* STANDARD INCLUDE FILES */
{mfdeclre.i}
{pxmaint.i}

/* Define Handles for the programs. */
{pxphdef.i rqgrsxr}
{pxphdef.i rqgrsxr1}
{pxphdef.i rqstdxr}
/* End Define Handles for the programs. */


/* ========================================================================== */
/* ******************************* MAIN BLOCK ******************************* */
/* ========================================================================== */


/* ========================================================================== */
/* *************************** INTERNAL PROCEDURES ************************** */
/* ========================================================================== */

/*============================================================================*/
PROCEDURE validateGRSRequisition :

/*------------------------------------------------------------------------------
Purpose:       Perform the necessary validations to ensure the GRS requisition
               can be used on a PO
Exceptions:    APP-ERROR-RESULT
Conditions:
        Pre:   None
        Post:  rqm_mstr(r), rqd_det(r)
Notes:
History:
------------------------------------------------------------------------------*/
   define input parameter pRequisitionId as character no-undo.
   define input parameter pRequisitionLineId as integer no-undo.
   define output parameter pRequisitionPart as character no-undo.

   define buffer rqm_mstr  for rqm_mstr.
   define buffer rqd_det   for rqd_det.

   do on error undo, return error {&GENERAL-APP-EXCEPT}:

      /* Validate the Requisition Master record */

      /* Get the GRS Requisition master record in scope */
      {pxrun.i &PROC='processRead' &PROGRAM='rqgrsxr.p'
                &HANDLE=ph_rqgrsxr
               &PARAM="(input pRequisitionId,
                        buffer rqm_mstr,
                        input FALSE,
                        input FALSE)"}

      if return-value <> {&SUCCESS-RESULT} then do:
         if return-value = {&RECORD-NOT-FOUND} then do:
            /*REQUISITION DOES NOT EXIST*/
            {pxmsg.i &MSGNUM=193 &ERRORLEVEL={&APP-ERROR-RESULT}}
            return error {&APP-ERROR-RESULT}.
         end.
         else do:
            return return-value.
         end.
      end.

      /*ENSURE REQUISITION STATUS IS OPEN*/
      {pxrun.i &PROC='validateReqIsOpen' &PROGRAM='rqgrsxr.p'
                &HANDLE=ph_rqgrsxr
               &PARAM="(input rqm_nbr)"}

      /*ENSURE REQUISITION ROUTED TO PURCHASING*/
      {pxrun.i &PROC='validateReqRoutedToPurchasing' &PROGRAM='rqgrsxr.p'
                &HANDLE=ph_rqgrsxr
               &PARAM="(input rqm_nbr)"}

      /* Validate the Requisition Line record */

      /* Get the GRS Requisition Line record in scope */
      {pxrun.i &PROC='processRead' &PROGRAM='rqgrsxr1.p'
                &HANDLE=ph_rqgrsxr1
               &PARAM="(input pRequisitionId,
                        input pRequisitionLineId,
                        buffer rqd_det,
                        input FALSE,
                        input FALSE)"}

      if return-value <> {&SUCCESS-RESULT} then do:
         if return-value = {&RECORD-NOT-FOUND} then do:
            /*LINE ITEM DOES NOT EXIST*/
            {pxmsg.i &MSGNUM=45 &ERRORLEVEL={&APP-ERROR-RESULT}}
            return error {&APP-ERROR-RESULT}.
         end.
         else do:
            return return-value.
         end.
      end.

      /*ENSURE REQ LINE IS OPEN*/
      {pxrun.i &PROC='validateReqLineIsOpen' &PROGRAM='rqgrsxr1.p'
                &HANDLE=ph_rqgrsxr1
               &PARAM="(input rqd_nbr, input rqd_line)"}

      /*ENSURE REQ LINE IS APPROVED*/
      {pxrun.i &PROC='validateReqLineIsApproved' &PROGRAM='rqgrsxr1.p'
                &HANDLE=ph_rqgrsxr1
         &PARAM="(input rqd_nbr, input rqd_line)"}

      pRequisitionPart = rqd_part.
   end.

   return {&SUCCESS-RESULT}.

END PROCEDURE.

/*============================================================================*/
PROCEDURE validateRequisition :

/*------------------------------------------------------------------------------
Purpose:       Ensure that requisition selected is valid for use on a PO. Both
               Standard and Global requisitions are validated.
Exceptions:    NONE
Conditions:
        Pre:   None.
        Post:  None.
Notes:
History:
------------------------------------------------------------------------------*/
   define input parameter pRequisitionId as character no-undo.
   define input parameter pRequisitionLineId as integer no-undo.
   define output parameter pRequisitionPart as character no-undo.

   do on error undo, return error {&GENERAL-APP-EXCEPT}:

      /* Validate the GRS Requisition */
      {pxgetph.i rqgrsxr}

      if {pxfunct.i &FUNCTION='isGRSINUse' &PROGRAM='rqgrsxr.p'} then do:

         /* Get the Requisition record in scope */
         {pxrun.i &PROC='validateGRSRequisition' &PARAM="(
            input pRequisitionId,
            input pRequisitionLineId,
            output pRequisitionPart)"}

      end.
      else do:
         /* Validate the Standard Requisition */

         /* Get the Requisition record in scope */
         {pxrun.i &PROC='validateStandardRequisition' &PARAM="(
            input pRequisitionId,
            output pRequisitionPart)"}
      end.

   end.

   return {&SUCCESS-RESULT}.

END PROCEDURE.

/*============================================================================*/
PROCEDURE validateRequisitionItem :

/*------------------------------------------------------------------------------
Purpose:       Validate if PO Line Item differs from Requisition Item.
Exceptions:    WARNING-RESULT
Conditions:
        Pre:   None
       Post:   None
Notes:
History:       Extracted from popomtea.p
------------------------------------------------------------------------------*/
   define input parameter pItemId            as character no-undo.
   define input parameter pRequisitionId     as character no-undo.
   define input parameter pRequisitionItemId as character no-undo.

   do on error undo, return error {&GENERAL-APP-EXCEPT}:
      if pRequisitionId > "" and pItemId <> pRequisitionItemId then do:

         /* MESSAGE #303 - REQUISTION NUMBER DOES NOT EXIST FOR ITEM*/
         {pxmsg.i
            &MSGNUM=303
            &ERRORLEVEL={&WARNING-RESULT}}
         return {&WARNING-RESULT}.
      end.
   end.

   return {&SUCCESS-RESULT}.

END PROCEDURE.

/*============================================================================*/
PROCEDURE validateStandardRequisition :

/*------------------------------------------------------------------------------
Purpose:       Perform the necessary validations to ensure the standard
               requisition can be used on a PO
Exceptions:    APP-ERROR-RESULT
Conditions:
        Pre:   None
        Post:  req_det(r)
Notes:
History:
------------------------------------------------------------------------------*/
   define input parameter pRequisitionId as character no-undo.
   define output parameter pRequisitionPart as character no-undo.

   define buffer req_det for req_det.

   do on error undo, return error {&GENERAL-APP-EXCEPT}:

      /* Get the Standard Requisition record in scope */
      {pxrun.i &PROC='processRead' &PROGRAM='rqstdxr.p'
                &HANDLE=ph_rqstdxr
               &PARAM="(input pRequisitionId,
                        buffer req_det,
                        input FALSE,
                        input FALSE)"}

      if return-value <> {&SUCCESS-RESULT} then do:
         if return-value = {&RECORD-NOT-FOUND} then do:
            /*REQUISITION DOES NOT EXIST*/
            {pxmsg.i &MSGNUM=193 &ERRORLEVEL={&APP-ERROR-RESULT}}
            return error {&APP-ERROR-RESULT}.
         end.
         else do:
            return return-value.
         end.
      end.

      /*ENSURE REQUISITION IS APPROVED*/
      {pxrun.i &PROC='validateReqIsApproved' &PROGRAM='rqstdxr.p'
                &HANDLE=ph_rqstdxr
               &PARAM="(input req_nbr)"}

      /*WARN IF REQ IS BEING USED ON BLANKET PO*/
      {pxrun.i &PROC='validateReqNotOnBlanket'  &PROGRAM='rqstdxr.p'
                &HANDLE=ph_rqstdxr
               &PARAM="(input req_nbr)"}

      pRequisitionPart = req_part.
   end.

   return {&SUCCESS-RESULT}.

END PROCEDURE.
